package j2024.j202407;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class j0730 {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.nextLine();
        String b = in.nextLine();
        int[] ca = new int[26];
        int[] cb = new int[26];
        for (int i = 0; i < a.length(); i++) {
            ca[(a.charAt(i)-'A')]++;
        }
        for (int i = 0; i < b.length(); i++) {
            cb[(b.charAt(i)-'A')]++;
        }
        boolean ret = true;
        for (int i = 0; i < 26; i++) {
            if((ca[i]==0 && cb[i]>0) || cb[i]>ca[i]){
                ret = false;
                break;
            }
        }
        if(ret==true){
            System.out.println("Yes");
        }else {
            System.out.println("No");
        }
    }

    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[3*n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = in.nextInt();
        }
        Arrays.sort(arr);
        long ret = 0;
        for (int i = arr.length-2; i >= n; i-=2) {
            ret+=arr[i];
        }
        System.out.println(ret);
    }

    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int[] arr = new int[n];
            int[] hash = new int[10001];

            for (int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
                hash[arr[i]]+=arr[i];
            }

            int[] select = new int[10001];
            int[] unsel = new int[10001];
            for (int i = 1; i <= 10000; i++) {
                select[i] = hash[i] + unsel[i-1];
                unsel[i] = Math.max(select[i-1],unsel[i-1]);
            }
            System.out.println(Math.max(select[10000],unsel[10000]));
    }

    /**
     * 1863. 找出所有子集的异或总和再求和
     */
    int path;
    int sum;
    public int subsetXORSum(int[] nums) {
        dfs(nums,0);
        return sum;
    }
    public void dfs(int[] nums,int pos){
        sum+=path;
        for(int i = pos;i < nums.length;i++){
            path ^= nums[i];
            dfs(nums,i+1);
            path ^= nums[i];
        }
    }

    /**
     * 47. 全排列 II
     */
    List<List<Integer>> ret;
    List<Integer> ppath;
    boolean[] vis;
    public List<List<Integer>> permuteUnique(int[] nums) {
        ret = new ArrayList<>();
        vis = new boolean[nums.length];
        ppath = new ArrayList<>();
        Arrays.sort(nums);
        Dfs(nums,0);
        return ret;
    }
    public void Dfs(int[] nums,int pos){
        if(pos ==  nums.length){
            ret.add(new ArrayList<>(ppath));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if(vis[i]== false && (i==0 || nums[i]!=nums[i-1] ||  vis[i-1]!=false)){
                ppath.add(nums[i]);
                vis[i] =  true;
                Dfs(nums,pos+1);
                ppath.remove(ppath.size()-1);
                vis[i] = false;
            }
        }
    }



}
